<template>
  <!-- <h2>首页:路径起别名：webpack.base.conf.js:re-solve</h2> -->
  <div id="home">
    <nav-bar class="home-nav">
      <template v-slot:center>
        <div @click="reload">购物街</div>
      </template>
    </nav-bar>
    <!-- 代替吸顶效果 -->
    <tab-control
      :titles="['流行', '精品', '新款']"
      @tabClick="tabClick"
      ref="tabControlTop"
      class="tab-control-top"
      v-show="isTabFixed"
    />

    <scroll
      id="content"
      :probe-type="3"
      :isPullUpLoad="true"
      @scroll="contentScroll"
      @pullingUp="loadMore"
      ref="scrollRef"
    >
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        :titles="['流行', '精品', '新款']"
        @tabClick="tabClick"
        ref="tabControl"
      />
      <goods-list :goods="showGoods" />
    </scroll>

    <!-- 监听组件是否被点击 -->
    <back-top v-show="isShowBackTop" @click.native="backTop" />
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView.vue";
import TabControl from "components/content/tabcontrol/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
// import BackTop from "components/content/backtop/BackTop";
import { backTopMixin } from "common/mixin";

import { getHomeMultidata, getHomeGoods } from "network/home";
// import { debounce } from "common/utils";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
  },
  mixins: [backTopMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  ////////vue3中keep-alive能够使用，如果不能使用，则需要记录离开时状态，以下未原生keep-alive
  destroyed() {
    // console.log("home destroyed");
  },
  activated() {
    //组件调用时回调，展示离开时的位置
    // console.log("home activated");
    //刷新一下
    this.$refs.scrollRef.refresh();
    this.$refs.scrollRef.scrollTo(0, this.saveY, 0);
    // console.log("scrollTo");
  },
  deactivated() {
    // 组件停止调用时回调，即切换菜单时，记录离开时页面位置
    // console.log("home deactivated");
    this.saveY = this.$refs.scrollRef.getScrollY();
    this.$refs.scrollRef.refresh();
  },
  created() {
    //1.请求多个数据
    this.getHomeMultidata();
    //2.请求商品
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
    //不要created中拿ddocument.querySelector和this.$refs等，有可能拿不到
  },
  mounted() {
    //3.监听图片加载完成+防抖
    // const refresh = debounce(this.$refs.scrollRef.refresh, 50);
    // this.$bus.$on("itemImageLoad", () => {
    //   refresh();
    // });
    //已用observeImage: true,
    //3.上拉置顶,通过offsetTop获取tabControl吸顶位置
    //所有组件都有一个属性$el，用于获取组件中的元素
    // console.log(this.$refs.tabControl.$el.offsetTop);
    // location = location;
  },

  methods: {
    //////////////////////////////////////////////事件监听相关的方法/////////////////////////////////////

    tabClick(index) {
      // console.log(index);
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControlTop.currentIndex = index;
      this.$refs.tabControl.currentIndex = index;
    },
    backTop() {
      // this.$refs.scrollRef.scroll.scrollTo(0, 0, 1000);
      //封装函数后
      this.$refs.scrollRef.scrollTo(0, 0);
    },
    contentScroll(position) {
      // console.log(position);
      //1.判断BackTop是否显示
      this.isShowBackTop = -position.y > 2000;
      //2.决定tabControl是否吸顶
      this.isTabFixed = -position.y > this.tabOffsetTop;
      // this.isTabFixed = -position.y > 533;
      // console.log(this.tabOffsetTop);
      // this.$refs.scrollRef.refresh();
    },
    loadMore() {
      // console.log("上拉加载更多");
      this.getHomeGoods(this.currentType);
      //加载完一张图需要刷新，下拉长度重新计算scrollHeight
    },
    swiperImageLoad() {
      // console.log(this.$refs.tabControl.$el.offsetTop);

      setTimeout(() => {
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop;
        // console.log(this.tabOffsetTop);
      }, 100);
    },

    reload() {
      window.location.reload();
    },
    //////////////////////////////////////////////网络请求相关的方法/////////////////////////////////////
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        // console.log(res);
        //数据进一步处理
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[page] += 1;
      });
    },
  },
};
</script>

<style scoped>
#home {
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  font-weight: 700;
  color: #fff;

  /*在使用浏览器原生滚动时，为了让导航栏不一起滚动而设置的
   position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9; */
}

#content {
  position: absolute;
  top: 44px;
  /* 滚动必须给高度 */
  /* css3函数calc(两边留空格) */
  height: calc(100% - 93px);

  /* 或者用绝对定位 */
}

.tab-control-top {
  position: relative;
  z-index: 9;
}
</style>